package com.example.zyfypt516wh.fragments;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.apdater.ArticleAdapter;

import com.example.zyfypt516wh.bean.ResBean;

import com.example.zyfypt516wh.iface.ResListener;


import com.example.zyfypt516wh.model.ResModel;

import java.util.List;

/*收藏文章列表*/
public class Fragment1 extends BaseFragments{
    private List<ResBean> list;
    private RecyclerView recyclerView;
    private ArticleAdapter adapter;

    private LinearLayoutManager layoutManager;//显示布局效果
    private int page = 1;// 代表页数，并初始化为 1，代表第 1
    private int lastVisibleItemPosition;//滚动刷新时，最后一条可见条目的位置

    private ResListener listener = new ResListener() {
        @Override
        public void onResponse(List<ResBean> beanlist) {
            if(page==1)//第一页直接赋值
            {
                list=beanlist;
            }
            else {//其他页需要更新替换
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }adapter.setList(list);
        }
        @Override public void onFail(String msg) {
            Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();
            System.out.println("--"+msg);
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        return  inflater.inflate(R.layout.fragment1,container,false);


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--fragment1--"+getSessionID());

        initRV(view);
        ResModel model = new ResModel();
        model.getResultList("article",page,getSessionID(),listener);

    }

    private void initRV(View view) {
        recyclerView = view.findViewById(R.id.rv1);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);//设置列表为线性列表
        recyclerView.setHasFixedSize(true);
        adapter = new ArticleAdapter(context);
        recyclerView.setAdapter(adapter);//将控件和适配器绑定
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (list!=null){
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size())
                    {
                        page += 1;

                        ResModel model = new ResModel();
                        model.getResultList("article",page,getSessionID(),listener);
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            }
        });
    }
}